/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ControllerAlumno;


import Modelo.Alumno;
import Modelo.Pregunta;
import Modelo.RegistroDetallesExamenes;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author JEANPIERE
 */
@WebServlet(name = "ServletCorregirExamen", urlPatterns = {"/ServletCorregirExamen"})
public class ServletCorregirExamen extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType( "text/html; charset=iso-8859-1" );
        PrintWriter out = response.getWriter();
        HttpSession sesion=request.getSession(); 
        RegistroDetallesExamenes registroDetallesExamenes = new RegistroDetallesExamenes();
        ArrayList<Pregunta> preguntas = ( ArrayList<Pregunta>) sesion.getAttribute("preguntas");
        Alumno alumno = (Alumno) sesion.getAttribute("alumno");
        String codigoExamen = (String) sesion.getAttribute("codigoExamen");
          
         
       String param = request.getParameter("preguntas");
       String[][] d;
        d = new Gson().fromJson(param,String[][].class);
    
          
          int nota=0;
          for(int i=0;i<preguntas.size();i++){
             

              if( preguntas.get(i).verificarPregunta(d[i])){
                   nota=nota+20;
         
        }
         }
     
       Calendar fecha = new GregorianCalendar();
        //Obtenemos el valor del año, mes, día,
        //hora, minuto y segundo del sistema
        //usando el método get y el parámetro correspondiente
        int año = fecha.get(Calendar.YEAR);
        int mes = fecha.get(Calendar.MONTH)+1;
        int dia = fecha.get(Calendar.DAY_OF_MONTH);
       String fechae=dia+"/"+mes+"/"+año;
     
       
      
       registroDetallesExamenes.registrar(codigoExamen, alumno.getCodigo(), nota,fechae);
                
      
            
       out.println("<h1>"+nota+"</h1>"); 
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
